#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"


void Initialize(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}


void Destroyed(SL* ps)
{
	if (ps->arr) {
		ps->size = 0;
		free(ps);
		ps = NULL;
	}

}

void CheckMemory(SL* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int NewCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLdatatype* tmp = (SLdatatype*)realloc(ps->arr, sizeof(SLdatatype) * NewCapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->arr = tmp;
		ps->capacity = NewCapacity;
	}
}



void SLPushBack(SL* ps, SLdatatype x)
{
	assert(ps);
	CheckMemory(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}

//void Print(SL* ps)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->arr[i]);
//	}
//	printf("\n");
//}


void SLPushFront(SL* ps, SLdatatype x)
{
	assert(ps);
	assert(ps->size);
	CheckMemory(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

void SLDesPush(SL* ps, int i, SLdatatype x)
{
	assert(ps);
	assert(i >= 0 && i <= ps->size);
	for (int j = ps->size - 1; j >= i - 1; j--)
	{
		ps->arr[j + 1] = ps->arr[j];
	}
	ps->arr[i - 1] = x;
	ps->size++;
}



void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 1; i < ps->size; i++)
	{
		ps->arr[i - 1] = ps->arr[i];
	}
	ps->size--;
}


void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}


void SLDesPop(SL* ps, int i)
{
	assert(ps);
	assert(i >= 0 && i < ps->size);
	for (i; i < ps->size; i++)
	{
		ps->arr[i - 1] = ps->arr[i];
	}
	ps->size--;
}

//
//int SLFind(SL* ps, SLdatatype x)
//{
//	assert(ps);
//	assert(ps->size);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}


void SLRevise(SL* ps, int i, SLdatatype x)
{
	assert(ps);
	assert(i >= 0 && i < ps->size);
	ps->arr[i - 1] = x;
}